<?php
namespace Manage\Model;
use Think\Model;
class PermissionModel extends Model{
	public function operPermission($data,$permission_id=0){
		$data['description']=json_encode($data['description']);
		if($permission_id){
			$this->where(array('permission_id'=>$permission_id))->save($data);
		}else{
			$this->add($data);
		}
	}
	public function getPermission($permission_id){
		$permission=$this->where(array('permission_id'=>$permission_id))->find();
		$permission['description']=json_decode($permission['description'],true);
		return $permission;
	}
	public function getPermissions($data=array()){
		$map=array();
		if(isset($data['permission_id'])) $map['permission_id']=$data['permission_id'];
		if(isset($data['parent_id']))  $map['parent_id']=$data['parent_id'];
		$perms=$this->where($map)->select();
		$return=array();
		foreach ($perms as $k=>$v) {
			$description=json_decode($v['description'],true);
			$return[]=array(
				'permission_id'	=>	$v['permission_id'],
				'parent_id'		=>	$v['parent_id'],
				'name'			=>	$v['name'],
				'title'			=>	$description[get_default_lng_id()]['title'],
				);
		}
		return $return;
	}
}